package com.qkl.xiatang.app.web.controller;

import com.qkl.xiatang.app.dto.feed.WeighGroupQtyInDTO;
import com.qkl.xiatang.app.dto.pond.AddPondGroupDTO;
import com.qkl.xiatang.app.dto.wvp.*;
import com.qkl.xiatang.app.service.feed.FeedWeighService;
import com.qkl.xiatang.app.service.wvp.WvpService;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.ErrorCodeUtils;
import com.qkl.xiatang.app.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class WvpController {

    @Autowired
    private WvpService wvpService;


    /**
     *  监控查询
     * @param params
     * @return
     */
    @GetMapping("/queryMonitorList")
    public Result queryMonitorList(@RequestParam Map<String,String> params) {
        String pageIndex = StringUtils.trimToEmpty(params.get("pageIndex"));
        String pageSize = StringUtils.trimToEmpty(params.get("pageSize"));

        MonitorQueryInDTO dto = new MonitorQueryInDTO();
        dto.setPageIndex(AppUtils.parseInt(pageIndex));
        dto.setPageSize(AppUtils.parseInt(pageSize));
        return ControllerHelper.toQueryResult(wvpService.queryMonitorList(dto));
    }

    /**
     *  监控明细查询
     * @param params
     * @return
     */
    @GetMapping("/queryMonitorDetail")
    public Result queryMonitorDetail(@RequestParam Map<String,String> params) {
        String deviceId = StringUtils.trimToEmpty(params.get("deviceId"));
        String channelId = StringUtils.trimToEmpty(params.get("channelId"));

        MonitorQueryInDTO dto = new MonitorQueryInDTO();
        dto.setDeviceId(deviceId);
        dto.setChannelId(channelId);
        return ControllerHelper.toQueryResult(wvpService.queryMonitorDetail(dto));
    }

    /**
     * 通用前端控制命令
     * @param webDTO
     * @return
     */
    @PostMapping("/controlFrontEnd")
    public Result controlFrontEnd(@RequestBody ControlFrontEndDTO webDTO) {
        log.info("=======controlFrontEnd.webDTO={}",webDTO);
        return ControllerHelper.toOperateResult(wvpService.controlFrontEnd(webDTO));
    }

    /**
     * 云台控制
     * @param webDTO
     * @return
     */
    @PostMapping("/controlCloud")
    public Result controlFrontEnd(@RequestBody ControlCloudDTO webDTO) {
        log.info("=======controlCloud.webDTO={}",webDTO);
        return ControllerHelper.toOperateResult(wvpService.controlCloud(webDTO));
    }

    /**
     * 录像查询
     * @param params
     * @return
     */
    @GetMapping("/playBackRecordQuery")
    public Result playBackRecordQuery(@RequestParam Map<String,String> params) {
        log.info("=======controlCloud.recordQuery={}",params);
        String channelId = StringUtils.trimToEmpty(params.get("channelId"));
        String startTime = StringUtils.trimToEmpty(params.get("startTime"));
        String endTime = StringUtils.trimToEmpty(params.get("endTime"));

        PlayBackRecordQueryDTO dto = new PlayBackRecordQueryDTO();
        dto.setStartTime(startTime);
        dto.setChannelId(channelId);
        dto.setEndTime(endTime);
        return ControllerHelper.toQueryResult(wvpService.playBack(dto));
    }

    /**
     * 开始视频回放
     * @param params
     * @return
     */
    @GetMapping("/playBackStart")
    public Result playBackStart(@RequestParam Map<String,String> params) {
        log.info("=======controlCloud.playBackStart={}",params);
        String channelId = StringUtils.trimToEmpty(params.get("channelId"));
        String startTime = StringUtils.trimToEmpty(params.get("startTime"));
        String endTime = StringUtils.trimToEmpty(params.get("endTime"));

        PlayBackStartDTO dto = new PlayBackStartDTO();
        dto.setStartTime(startTime);
        dto.setChannelId(channelId);
        dto.setEndTime(endTime);
        return ControllerHelper.toQueryResult(wvpService.playBack(dto));
    }

    /**
     * 停止视频回放
     * @param params
     * @return
     */
    @GetMapping("/playBackStop")
    public Result playBackStop(@RequestParam Map<String,String> params) {
        log.info("=======controlCloud.playBackStop={}",params);
        String channelId = StringUtils.trimToEmpty(params.get("channelId"));
        String stream = StringUtils.trimToEmpty(params.get("stream"));

        PlayBackStopDTO dto = new PlayBackStopDTO();
        dto.setStream(stream);
        dto.setChannelId(channelId);
        return ControllerHelper.toQueryResult(wvpService.playBack(dto));
    }

    /**
     * 回放倍速播放
     * @param params
     * @return
     */
    @GetMapping("/playBackSpeed")
    public Result playBackSpeed(@RequestParam Map<String,String> params) {
        log.info("=======controlCloud.playBackSpeed={}",params);
        String streamId = StringUtils.trimToEmpty(params.get("streamId"));
        String speed = StringUtils.trimToEmpty(params.get("speed"));

        PlayBackSpeedDTO dto = new PlayBackSpeedDTO();
        dto.setSpeed(AppUtils.parseDouble(speed));
        dto.setStreamId(streamId);
        return ControllerHelper.toQueryResult(wvpService.playBack(dto));
    }

    /**
     * 回放拖动播放
     * @param params
     * @return
     */
    @GetMapping("/playBackSeek")
    public Result playBackSeek(@RequestParam Map<String,String> params) {
        log.info("=======controlCloud.playBackSeek={}",params);
        String seekTime = StringUtils.trimToEmpty(params.get("seekTime"));
        String streamId = StringUtils.trimToEmpty(params.get("streamId"));

        PlayBackSeekDTO dto = new PlayBackSeekDTO();
        dto.setSeekTime(AppUtils.parseInt(seekTime));
        dto.setStreamId(streamId);
        return ControllerHelper.toQueryResult(wvpService.playBack(dto));
    }

    /**
     * 回放恢复
     * @param params
     * @return
     */
    @GetMapping("/playBackResume")
    public Result playBackResume(@RequestParam Map<String,String> params) {
        log.info("=======controlCloud.playBackResume={}",params);
        String streamId = StringUtils.trimToEmpty(params.get("streamId"));

        PlayBackResumeDTO dto = new PlayBackResumeDTO();
        dto.setStreamId(streamId);
        return ControllerHelper.toQueryResult(wvpService.playBack(dto));
    }

    /**
     * 回放暂停
     * @param params
     * @return
     */
    @GetMapping("/playBackPause")
    public Result playBackPause(@RequestParam Map<String,String> params) {
        log.info("=======controlCloud.playBackPause={}",params);
        String streamId = StringUtils.trimToEmpty(params.get("streamId"));

        PlayBackPauseDTO dto = new PlayBackPauseDTO();
        dto.setStreamId(streamId);
        return ControllerHelper.toQueryResult(wvpService.playBack(dto));
    }


}
